package br.univas.vperiod.jowey.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import br.univas.vperiod.jowey.dao.IProjectDAO;
import br.univas.vperiod.jowey.dao.ITimeReportDAO;
import br.univas.vperiod.jowey.entities.PlayerJob;
import br.univas.vperiod.jowey.entities.TimeReport;
import br.univas.vperiod.jowey.service.SchedulerTimeReport;

@Controller
@RequestMapping("timeReport")
public class TimeReportController {
	
	
		@Autowired
		private ITimeReportDAO<?> timeReportDao;
		
		@Autowired
		private IProjectDAO<?> projectDao;
		
		
		
		@RequestMapping("/start")
		public String start(){
			try {
				SchedulerTimeReport.startScheduler();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "timeReport";
		}
		
		@RequestMapping("/open")
		public String openTimeReport(){
			return "timeReportOpen";
		}
		
		@RequestMapping("/list")
		public String home(){
			Calendar c = Calendar.getInstance();
			Date date = new Date("2012/02/01");
			c.setTime(date);
			
			System.out.println(c.get(Calendar.WEEK_OF_YEAR));
			System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
			return "timeReport";
		}
		
		@RequestMapping("/listPlayerReport")
		public String listPlayerReport(){
			return "userReport";
		}
		
		@RequestMapping("/getTimeReports")
		@ResponseBody
		public ArrayList<TimeReport> getTimeReports(){
			ArrayList<TimeReport> timeReports = new ArrayList<TimeReport>();
			timeReports = timeReportDao.findAll();
			
			for(TimeReport tr : timeReports){
				tr.setWeekFrom(tr.getWeeks().get(0).getNumber());
				tr.setWeekTo(tr.getWeeks().get(tr.getWeeks().size() -1).getNumber());
				tr.setWeeks(null);
			}
			return timeReports;
		}
		
		
		@RequestMapping(value = "/playerReportOpen/{idPlayer}",method=RequestMethod.GET)
		public ModelAndView reportOpen(@PathVariable("idPlayer")int idPlayer, ModelMap model){
			model.put("idPlayer", idPlayer);
			return new ModelAndView("playerReportOpen",model);
		}
		
		@RequestMapping(value = "/getProjectsByPlayer/{idPlayer}",method=RequestMethod.GET)
		@ResponseBody
		public ArrayList<PlayerJob> getProjectsByPlayer(@PathVariable("idPlayer")int idPlayer){
			ArrayList<PlayerJob> playerJobs = new ArrayList<PlayerJob>();
			playerJobs =  projectDao.getPlayersInProjects(idPlayer);
			
			return playerJobs;
		}
		
}
